package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品套餐相关接口
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController {
    @Autowired
    private SetmealService setmealService;

    // 新增套餐接口
    @PostMapping
    @ApiOperation("新增套餐接口")
    // TODO @CacheEvict()
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.saveSetMeal(setmealDTO);
        return Result.success();
    }
    //套餐分页查询接口
    @GetMapping("/page")
    @ApiOperation("套餐分页查询接口")
    public Result<PageResult> page(@ModelAttribute SetmealPageQueryDTO setmealPageQueryDTO){
            PageResult setmeals = setmealService.page(setmealPageQueryDTO);
            return Result.success(setmeals);
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐接口")
    @CacheEvict(value = "setmeal",allEntries = true) //清理全部缓存
    public Result deleteBatch(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐接口")
    @CacheEvict(value = "setmeal",allEntries = true) //清理全部缓存
    public Result updateSetMeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetMealInfo(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐接口")
    public Result<SetmealVO> getSetMealById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getSetMealById(id);
        return Result.success(setmealVO);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售与停售")
    @CacheEvict(value = "setmeal",allEntries = true) //清理全部缓存
    public Result SetMealStatus(@RequestParam Long id ,@PathVariable int status){
        setmealService.setSetMealStatus(id,status);
        return Result.success();
    }
}
